<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class mmenu extends MY_Model {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
    public $mytable = "menu";
    public $primaryKey = "men_id";
    public $resCode = "MENU";
	
	public $type = array(
		'url' => 'Đường dẫn url',
		'cat' => 'Danh mục'
	);
	
    function __construct() {
        parent::__construct();
		
    }

    function field() {
		$fieldData = array();
	$fieldData['men_name'] = '';
	$fieldData['men_type'] = '';
	$fieldData['men_parent'] = 0;
	$fieldData['men_value'] = '';
	$fieldData['men_order'] = 0;
	$fieldData['men_status'] = 1;
	return $fieldData;
    }
	
	function get_parent($parent = 0, $status = 1) {
		$this->db->order_by('men_order asc');
		if ($status>=0) {
			$this->db->where('men_status', $status);
		}
		return $this->gets(array(
			'men_parent' => $parent, 
		));
	}
	
	 function buid_tree_cate($data, $parent = 0) {
		$menuresult = array();
		while(!empty($data)) {
			$menu = array();
			if ($data) foreach($data as $seq => $d) {
				if ($d->men_parent == $parent)  {
					$menu[$parent]  = $d;
					unset($data[$seq]);
				}
			}
			$parent = current($menu);
			$menuresult [] = $menu;
		}
	 }
	 
	
	function get_recursive(&$allmenu, $parent = 0) {
		$id_field = 'men_id';
		$parent_field = 'men_parent';
		//get current child 
		$menu = array();
		if ($allmenu && is_array($allmenu)) foreach ($allmenu as $k=>$v) {
			if ($v->{$parent_field} == $parent) {
				$menu[] = $v;
				unset($allmenu[$k]);
			}
		}
		if ($menu) foreach ($menu as &$item) {
			$temp_menu = $this->get_recursive($allmenu, $item->{$id_field});
			if ($temp_menu) {
				$item->child = $temp_menu;
			}
		}
		return $menu;
	}
	
	/*
	 * public function get_recursive($parent = 0) {
		$this->db->order_by('men_parent asc');
		$this->db->order_by('men_order asc');
		$allmenu = $this->gets();
		$menu =$this->buid_tree_cate($allmenu, $parent);
		prd($menu);
		// if ($menu) foreach ($menu as &$item) {
		// $item = (array) $item;
		// $temp_menu = $this->get_recursive($item['men_id']);
		// if (!isset($item['child'])) 
		// $item['child'] = array();
		// $item['child'] = array_merge($item['child'], $temp_menu);
		// }
		return $menu;
		}
	 * */
	public function get_recursive2($parent = 0) {
		$this->db->order_by('men_parent asc');
		$this->db->order_by('men_order asc');
		$menu = $this->gets(array('men_parent'=>$parent));
		
		if ($menu) foreach ($menu as &$item) {
			$item = (array) $item;
			$temp_menu = $this->get_recursive($item['men_id']);
			if (!isset($item['child'])) 
				$item['child'] = array();
			$item['child'] = array_merge($item['child'], $temp_menu);
		}
		return $menu;
	}
	public function update_recursive($menus, $parent = 0) {
		$data_update = array();
		$order = 1;
		if ($menus) foreach ($menus as $menu) { 
			if (isset($menu['id']) && $menu['id']) {
				$data_update['men_parent'] = $parent;
				$data_update['men_order'] = $order++;
				$this->update($menu['id'], $this->primaryKey, $data_update);
				// echo 'updating ' .$menu['id']. '=>'.$parent ."\n";
			}
			if (isset($menu['children']) && $menu['children']) {
				$this->update_recursive($menu['children'], $menu['id']);
			}
		}
	}
	
	public function get_menu($parent = 0) {
		$this->db->order_by('men_parent asc');
		$this->db->order_by('men_order asc');
		$this->db->where('men_status >', 0);
		$all_menu = $this->gets();
		$result = $this->mmenu->get_recursive($all_menu, $parent);
		return $result;
		// return $this->scache->script('model.mmenu.get_menu.parent.'.$parent,'$this->get_recursive('.$parent.');');
		
	}
	
	/*
		Sắp xếp các danh mục để cache trong mảng
	*/
	function arrange_cate($menu_id = 0, &$cache_cat = array())
	{
		$menus = $this->get_menu($menu_id);
		$arr_cat_id = array();
		if(!$menus) return array();
		
		foreach($menus as $menu)
		{
			$arr_cat_id[] = $menu->men_value;
			if(isset($menu->child)) {
				foreach($menu->child as $child)
				{
					$arr_cat_id[] = $child->men_value;
				}
			}
		}
		$this->db->select('cat_code,cat_id,cat_name');
		$cats = $this->mcategory->where_in($arr_cat_id, 'cat_id');
		if($cats)
		{
			foreach($cats as $cat)
			{
				$cache_cat[$cat->cat_id] = $cat;
			}
		}
		return $menus;
	}
}
